package sparksql

import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, SparkSession}


object DataFrames {
  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession.builder()
      .appName(this.getClass.getSimpleName)
      .master("local[*]")
      .getOrCreate()
    val sc: SparkContext = spark.sparkContext
    val rdd: RDD[String] = sc.textFile("C:\\Users\\lynn\\Desktop\\name.txt")
    val rdd2: RDD[Person] = rdd.map(line => {
      val field: Array[String] = line.split(",")
      Person(field(0), field(1), field(2).toInt, field(3).toDouble)
    })
    //rdd转换为DataFrame
     import spark.implicits._
    val df: DataFrame = rdd2.toDF()
    df.select("*").orderBy("score").show()
    df.createTempView("v_person")
    spark.sql("select * from v_person where score=10.0").show()


  }
}


